package com.owlsmart.bluecat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.util.TimeUtils;
import androidx.recyclerview.widget.RecyclerView;

import com.code19.library.DateUtils;
import com.owlsmart.bluecat.R;
import com.owlsmart.bluecat.entity.MovieComment;
import com.owlsmart.bluecat.view.CircleTransform;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * @author mingtai
 * @description 写一个适配器，用来展示数据,每一个fragment都要有一个适配器
 * @time 2020/8/11 0:21
 * <p>
 * 本来是咨询适配器，现在改为影评适配器
 */

public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //上下文
    private Context mContext;
    //传过来的对象
    private List<MovieComment.MovieCommentDetail> datas;

    public void setDatas(List<MovieComment.MovieCommentDetail> datas) {
        this.datas = datas; //不用构造器，可以不用反复创建对象
    }

    private OnItemClickListener mOnItemClickListener;

    // 设置点击监听
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public NewsAdapter(Context context) {
        this.mContext = context;
    }

    public NewsAdapter(Context context, List<MovieComment.MovieCommentDetail> datas) {
        this.mContext = context;
        this.datas = datas;
    }


    // 写多种布局的时候，使用此方法
    @Override
    public int getItemViewType(int position) {
        // 如果image_list小于3个，就用3的布局
        int type = (datas.get(position).getCinecism().getImage_list().size()) == 3 ? 2 : 3;
        //返回的type会在onCreateViewHolder取出来
        return type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == 1) {
            // 加载第一个布局文件
            View view = LayoutInflater.from(mContext).inflate(R.layout.news_item_one, parent, false);
            return new ViewHolderOne(view);
        } else if (viewType == 2) {
            // 加载第2个布局文件
            View view = LayoutInflater.from(mContext).inflate(R.layout.news_item_two, parent, false);
            return new ViewHolderTwo(view);
        } else {
            // 加载第3个布局文件
            View view = LayoutInflater.from(mContext).inflate(R.layout.news_item_three, parent, false);
            return new ViewHolderThree(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //    绑定数据，给控件对象赋值。
        int type = getItemViewType(position);
        MovieComment.MovieCommentDetail movieCommentDetail = datas.get(position);
        // 添加不同的ViewHolderThree 绑定不同的数据
        if (type == 1) {
            ViewHolderOne vh = (ViewHolderOne) holder;
            vh.title.setText(movieCommentDetail.getCinecism().getTitle());
            vh.author.setText(movieCommentDetail.getCinecism().getUid_s());
            vh.comment.setText("评论");
            vh.time.setText("时间戳");
            vh.movieCommentDetail = movieCommentDetail;

            // 异步加载图片
            Picasso.with(mContext)
                    .load(movieCommentDetail.getCinecism().getCover_url())
                    .transform(new CircleTransform()) // 绘制圆形头像
                    .into(vh.header);
            //mThumb 缩略图

            Picasso.with(mContext).load("\"https://xfile2.a.88cdn.com/file/k/738239521/avatar/90o0.jpg/300x300\"")
                    .into(vh.thumb);

        } else if (type == 2) {
            ViewHolderTwo vh = (ViewHolderTwo) holder;

            vh.title.setText(movieCommentDetail.getCinecism().getTitle());
            vh.summary_title_two.setText(movieCommentDetail.getCinecism().getSummary());
            vh.create_time_show.setText(DateUtils.formatDate(movieCommentDetail.getCinecism().getCreate_time()*1000));
            vh.about_movie_show.setText(movieCommentDetail.getCinecism().getTag().isEmpty()?movieCommentDetail.getMedia_info().getName():movieCommentDetail.getCinecism().getTag());
            vh.movieCommentDetail = movieCommentDetail;
            // 异步加载图片
            // Picasso.with(mContext)
            //         .load("\"https://xfile2.a.88cdn.com/file/k/42985612/avatar/AbpGaw.jpg/300x300\"")
            //         .transform(new CircleTransform()) // 绘制圆形头像
            //         .into(vh.header);
            //mThumb 缩略图
            Picasso.with(mContext).load(movieCommentDetail.getCinecism().getImage_list().get(0))
                    .into(vh.pic1);//mThumb 缩略图
            Picasso.with(mContext).load(movieCommentDetail.getCinecism().getImage_list().get(1))
                    .into(vh.pic2);//mThumb 缩略图
            Picasso.with(mContext).load(movieCommentDetail.getCinecism().getImage_list().get(2))
                    .into(vh.pic3);

        } else {
            ViewHolderThree vh = (ViewHolderThree) holder;
            vh.title.setText(movieCommentDetail.getCinecism().getTitle());
            vh.summaryTitle.setText(movieCommentDetail.getCinecism().getSummary());
            vh.createTimeTwo.setText(DateUtils.formatDate(movieCommentDetail.getCinecism().getCreate_time()*1000));
            vh.aboutMovieTwo.setText(movieCommentDetail.getCinecism().getTag().isEmpty()?movieCommentDetail.getMedia_info().getName():movieCommentDetail.getCinecism().getTag());
            vh.movieCommentDetail = movieCommentDetail;
            // 异步加载图片
            // Picasso.with(mContext)
            //         .load("\"https://xfile2.a.88cdn.com/file/k/avatar/default/300x300\"")
            //         .transform(new CircleTransform()) // 绘制圆形头像
            //         .into(vh.header);
            //mThumb 缩略图
            Picasso.with(mContext).load(movieCommentDetail.getCinecism().getCover_url())
                    .into(vh.thumb);
        }

    }

    @Override
    public int getItemCount() {
        // recycleView 列表里面有多少项
        if (datas != null && datas.size() > 0) {
            return datas.size();
        }
        return 0;
    }

    // 写一个类，获取view对象中的每一条数据,然后在其他地方赋值。适配器的作用？
    public class ViewHolderOne extends RecyclerView.ViewHolder {
        //获取第一个布局的控件，然后在其他地方进行赋值
        private TextView title;
        private TextView author;
        private TextView comment;
        private TextView time;
        private ImageView header;
        private ImageView thumb;
        private MovieComment.MovieCommentDetail movieCommentDetail;

        public ViewHolderOne(@NonNull View view) {
            super(view);
            // 获得第一个布局所有控件对象
            title = view.findViewById(R.id.title);
            author = view.findViewById(R.id.author);
            comment = view.findViewById(R.id.comment);
            time = view.findViewById(R.id.time);
            header = view.findViewById(R.id.header);
            thumb = view.findViewById(R.id.thumb);
            // 设置一个监听事件，点击后要做的事情
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(movieCommentDetail);
                }
            });
        }
    }

    // 写一个类，获取view对象中的每一条数据
    public class ViewHolderTwo extends RecyclerView.ViewHolder {

        //获取第2个布局的控件，然后在其他地方进行赋值
        private TextView title;
        private TextView summary_title_two;
        private TextView create_time_show;
        private TextView about_movie_show;
        private ImageView pic1, pic2, pic3;
        private MovieComment.MovieCommentDetail movieCommentDetail;

        public ViewHolderTwo(@NonNull View view) {
            super(view);

            // 获得第2个布局所有控件对象
            title = view.findViewById(R.id.title);
            summary_title_two = view.findViewById(R.id.summary_title_two);
            create_time_show = view.findViewById(R.id.create_time_show);
            about_movie_show = view.findViewById(R.id.about_movie_show);
            pic1 = view.findViewById(R.id.pic1);
            pic2 = view.findViewById(R.id.pic2);
            pic3 = view.findViewById(R.id.pic3);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(movieCommentDetail);
                }
            });
        }
    }

    // 写一个类，获取view对象中的每一条数据
    public class ViewHolderThree extends RecyclerView.ViewHolder {
        //获取第3个布局的控件，然后在其他地方进行赋值
        private TextView title;
        private TextView summaryTitle;
        private TextView createTime;
        private TextView createTimeTwo;
        private TextView aboutMovieTwo;
        private TextView aboutMovie;
        private ImageView thumb;
        private MovieComment.MovieCommentDetail movieCommentDetail;

        public ViewHolderThree(@NonNull View view) {
            super(view);
            // 获得第3个布局所有控件对象
            title = view.findViewById(R.id.title);
            summaryTitle = view.findViewById(R.id.summary_title);
            createTime = view.findViewById(R.id.create_time);
            createTimeTwo = view.findViewById(R.id.create_time_two);
            aboutMovie = view.findViewById(R.id.about_movie);
            aboutMovieTwo = view.findViewById(R.id.about_movie_two);
            thumb = view.findViewById(R.id.thumb);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(movieCommentDetail);
                }
            });
        }
    }

    // 触发点击事件,搬运视频类的点击
    public interface OnItemClickListener {
        void onItemClick(MovieComment.MovieCommentDetail newsEntity);
    }


}
